/*
 *
 *
 *
 */
package com.hboxs.asl.controller.app;

import com.hboxs.asl.Filter;
import com.hboxs.asl.JsonMessage;
import com.hboxs.asl.controller.asl.BaseController;
import com.hboxs.asl.entity.Member;
import com.hboxs.asl.entity.VerificationCode;
import com.hboxs.asl.service.CaptchaService;
import com.hboxs.asl.service.MailService;
import com.hboxs.asl.service.MemberService;
import com.hboxs.asl.service.VerificationCodeService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Controller - 密码
 */
@Controller("appPasswordController")
@RequestMapping("/app/pw")
public class PasswordController extends BaseController {

    protected Logger logger = Logger.getLogger(getClass());

    @Resource(name = "captchaServiceImpl")
    private CaptchaService captchaService;
    @Resource(name = "memberServiceImpl")
    private MemberService memberService;
    @Resource(name = "mailServiceImpl")
    private MailService mailService;
    @Resource(name = "verificationCodeServiceImpl")
    private VerificationCodeService verificationCodeService;

    /**
     * 找回密码
     */
    @RequestMapping(value = "/find", method = RequestMethod.GET)
    public String find(Model model) {
        return "/asl/password/find";
    }

    /**
     * 找回密码提交
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public
    @ResponseBody
    JsonMessage find(String username, String code, String password) {
        try {
            //1.手机号是否为空，是否注册
            if (username == null) {
                return JsonMessage.error("手机号码不能为空");
            }
            Member member = memberService.findByUsername(username);
            if (member == null) {
                return JsonMessage.error("该手机号码未注册");
            }
            //2.验证码校验
            List<Filter> filters = new ArrayList<>();
            filters.add(Filter.eq("phone", username));
            List<VerificationCode> codes = verificationCodeService.findList(1, filters, null);
            String sendCode = null;
            if (codes != null) {
                for (VerificationCode verificationCode : codes) {
                    sendCode = verificationCode.getCode();
                }
            }
            if (!code.equalsIgnoreCase(sendCode)) {
                return JsonMessage.error("验证码不正确");
            }
            //4.密码不能为空
            if (password == null) {
                return JsonMessage.error("密码不能为空");
            }
            //3.更新member
            member.setPassword(password);
            memberService.update(member);

            return JsonMessage.success("密码重置成功");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return JsonMessage.UNKNOWN_ERROR;
    }


}